package user

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"github.com/gogf/gf/v2/errors/gerror"
	//"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/frame/g"
	//"github.com/gogf/gf-demo-user/v2/internal/model"
	"github.com/gogf/gf-demo-user/v2/internal/model/entity"
	"github.com/gogf/gf-demo-user/v2/api/web/v1"
	"github.com/gogf/gf-demo-user/v2/internal/service"
)

func (c *ControllerV1) Application(ctx context.Context, req *v1.ApplicationReq) (res *v1.ApplicationRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var Application *entity.Application
	err = g.Model("Application").Where("id", req.Application_id.String()).Scan(&Application)
	fmt.Println(err)
	res = &v1.ApplicationRes{
		Application,
	}
	return
}

func (c *ControllerV1) ApplicationPost(ctx context.Context, req *v1.ApplicationPostReq) (res *v1.ApplicationPostRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("ApplicationChatMessage").Data(g.Map{
		"id" : newUUID,
		"title" : req.Title,
		"company" : req.Company,
		"tag" : req.Tag,
		"description" : req.Description,
		"salary" : req.Salary,
		"isRemote" : req.IsRemote,
		"isHybrid" : req.IsHybrid,
		"expiry" : req.Expiry,
		"question" : req.Question,
		"status" : req.Status,
	}).Insert()
	fmt.Println(id)
	return
}

func (c *ControllerV1) ApplicationDele(ctx context.Context, req *v1.ApplicationDeleReq) (res *v1.ApplicationDeleRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("ApplicationChatMessage").Where("Application_id=?", account.Id).Delete()
	fmt.Println(id)
	return
}

func (c *ControllerV1) ApplicationUp(ctx context.Context, req *v1.ApplicationUpReq) (res *v1.ApplicationUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("ApplicationChatMessage").Data(g.Map{
		"title" : req.Title,
		"company" : req.Company,
		"tag" : req.Tag,
		"description" : req.Description,
		"salary" : req.Salary,
		"isRemote" : req.IsRemote,
		"isHybrid" : req.IsHybrid,
		"expiry" : req.Expiry,
		"question" : req.Question,
		"status" : req.Status,
	}).Where("id", req.Application_id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) ApplicationChatMessageUp(ctx context.Context, req *v1.ApplicationChatMessageUpReq) (res *v1.ApplicationChatMessageUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("ApplicationChatMessage").Data(g.Map{
		"status" : req.Status,
		"expiry" : req.Expiry,
	}).Where("id", req.Application_id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) ApplicationChatMessagePost(ctx context.Context, req *v1.ApplicationChatMessagePostReq) (res *v1.ApplicationChatMessageUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("ApplicationChatMessage").Data(g.Map{
		"question" : req.Question,
	}).Insert()
	fmt.Println(id)
	return
}

func (c *ControllerV1) ApplicationChatMessageDele(ctx context.Context, req *v1.ApplicationChatMessageDeleReq) (res *v1.ApplicationDeleRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("ApplicationChatMessage").Where("Application_id=?", account.Id).Delete()
	fmt.Println(id)
	return
}

func (c *ControllerV1) ApplicationChatMessage(ctx context.Context, req *v1.ApplicationChatMessageReq) (res *v1.ApplicationChatMessageRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var user *entity.ApplicationChatMessage
	err = g.Model("ApplicationChatMessage").Where("Application_id=?", account.Id).Scan(&user)
	fmt.Println(err)
	res = &v1.ApplicationChatMessageRes{
		user,
	}
	return
}


func (c *ControllerV1) ApplicationFilePost(ctx context.Context, req *v1.ApplicationFilePostReq) (res *v1.ApplicationChatMessageUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	// fmt.Println(account)
	fmt.Println(req.File)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("Application").Data(g.Map{
		"id" : newUUID,
		"resume" : req.File,
	}).Insert()
	fmt.Println(id)
	return
}

func (c *ControllerV1) ApplicationFileDele(ctx context.Context, req *v1.ApplicationFileDeleReq) (res *v1.ApplicationDeleRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("ApplicationChatMessage").Where("Application_id=?", account.Id).Delete()
	fmt.Println(id)
	return
}

func (c *ControllerV1) ApplicationFile(ctx context.Context, req *v1.ApplicationFileReq) (res *v1.ApplicationChatMessageRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var user *entity.ApplicationChatMessage
	err = g.Model("ApplicationChatMessage").Where("Application_id=?", account.Id).Scan(&user)
	fmt.Println(err)
	res = &v1.ApplicationChatMessageRes{
		user,
	}
	return
}




